import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("File Network Widget Demo"),
      ),
      body: HttpClientWidget(),
    ),
  ));
}

class HttpClientWidget extends StatefulWidget {
  const HttpClientWidget({Key? key}) : super(key:key);

  @override
  _HttpClientWidgetState createState() => _HttpClientWidgetState();
}

class _HttpClientWidgetState extends State<HttpClientWidget> {
  bool _loading = false;
  String _text = "";

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          ElevatedButton(
            onPressed: _loading ? null : request,
            child: const Text('获取百度首页'),
          ),
          Container(
            width: MediaQuery.of(context).size.width - 50.0,
            child: Text(_text.replaceAll(RegExp(r"\s"), "")),
          ),
        ],
      ),
    );
  }

  request() async {
    setState((){
      _loading = true;
      _text = "正在请求...";
    });
    try {
      HttpClient httpClient = HttpClient();
      // httpClient.addCredentials(_uri,
      //   "admin",
      //   HttpClientBasicCredentials("username","password"), //Basic认证凭据
      // );

//       SecurityContext sc = SecurityContext();
// //file为证书路径
//       sc.setTrustedCertificates(file);
      HttpClientRequest request = await httpClient.getUrl(Uri.parse("https://www.baidu.com"));
      // 使用Iphone的UA
      request.headers.add(
        "user-agent",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1",
      );
      //等待连接服务器（会将请求信息发送给服务器）
      HttpClientResponse response = await request.close();
      //读取响应内容
      _text = await response.transform(utf8.decoder).join();
      // 输出响应头
      print(response.headers);
      //关闭client后，通过该client发起的所有请求都会中止。
      httpClient.close();
    } catch (e) {
      _text = "请求失败 : $e";
    } finally {
      setState(() => _loading = false);
    }
  }
}